/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.model.gameobjects.player;

import com.aionemu.commons.database.dao.DAOManager;
import com.aionengine.gameserver.dao.PlayerPetsDAO;
import com.aionengine.gameserver.taskmanager.tasks.ExpireTimerTask;
import javolution.util.FastMap;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.List;

/**
 * @author ATracer
 */
public class PetList {

    private final Player player;
    private int lastUsedPetId;
    private FastMap<Integer, PetCommonData> pets = new FastMap<Integer, PetCommonData>();

    PetList(Player player) {
        this.player = player;
        loadPets();
    }

    public void loadPets() {
        List<PetCommonData> playerPets = DAOManager.getDAO(PlayerPetsDAO.class).getPlayerPets(player);
        PetCommonData lastUsedPet = null;
        for (PetCommonData pet : playerPets) {
            if (pet.getExpireTime() > 0) {
                ExpireTimerTask.getInstance().addTask(pet, player);
            }
            pets.put(pet.getPetId(), pet);
            if (lastUsedPet == null || pet.getDespawnTime().after(lastUsedPet.getDespawnTime()))
                lastUsedPet = pet;
        }

        if (lastUsedPet != null)
            lastUsedPetId = lastUsedPet.getPetId();
    }

    public Collection<PetCommonData> getPets() {
        return pets.values();
    }

    /**
     * @param petId
     * @return
     */
    public PetCommonData getPet(int petId) {
        return pets.get(petId);
    }

    public PetCommonData getLastUsedPet() {
        return getPet(lastUsedPetId);
    }

    public void setLastUsedPetId(int lastUsedPetId) {
        this.lastUsedPetId = lastUsedPetId;
    }

    /**
     * @param player
     * @param petId
     * @param decorationId
     * @param name
     * @return
     */
    public PetCommonData addPet(Player player, int petId, int decorationId, String name, int expireTime) {
        return addPet(player, petId, decorationId, System.currentTimeMillis(), name, expireTime);
    }

    public PetCommonData addPet(Player player, int petId, int decorationId, long birthday, String name, int expireTime) {
        PetCommonData petCommonData = new PetCommonData(petId, player.getObjectId(), expireTime);
        petCommonData.setDecoration(decorationId);
        petCommonData.setName(name);
        petCommonData.setBirthday(new Timestamp(birthday));
        petCommonData.setDespawnTime(new Timestamp(System.currentTimeMillis()));
        DAOManager.getDAO(PlayerPetsDAO.class).insertPlayerPet(petCommonData);
        pets.put(petId, petCommonData);
        return petCommonData;
    }

    /**
     * @param petId
     * @return
     */
    public boolean hasPet(int petId) {
        return pets.containsKey(petId);
    }

    /**
     * @param petId
     */
    public void deletePet(int petId) {
        if (hasPet(petId)) {
            pets.remove(petId);
            DAOManager.getDAO(PlayerPetsDAO.class).removePlayerPet(player, petId);
        }
    }
}
